unit F_ReportDocumentsByDate;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ZAbstractRODataset, ZDataset, Grids, DBGrids, JvExDBGrids,
  JvDBGrid, JvDBUltimGrid, StdCtrls, JvExControls, JvSpeedButton, ExtCtrls,
  JvDataSource, Mask, JvExMask, JvToolEdit, JvMaskEdit, JvCheckedMaskEdit,
  JvDatePickerEdit, IniFiles, JvDBLookup;

type
  TF_DocumentsByDate = class(TForm)
    Panel_Superior: TPanel;
    L_Buscar: TLabel;
    JvSB_Filtrar: TJvSpeedButton;
    GB_OpcionesDeFiltrado: TGroupBox;
    PanelInferior: TPanel;
    B_Cerrar: TJvSpeedButton;
    PanelGrilla: TPanel;
    JvDBUltimGrid: TJvDBUltimGrid;
    ZROQ_Documentos: TZReadOnlyQuery;
    JvDS_Documentos: TJvDataSource;
    Label_Desde: TLabel;
    JvDPE_Desde: TJvDatePickerEdit;
    Label1: TLabel;
    JvDPE_Hasta: TJvDatePickerEdit;
    Label2: TLabel;
    CB_FechaCreacion: TCheckBox;
    PanelOpciones: TPanel;
    B_AgregarMaterias: TJvSpeedButton;
    CB_Materia: TCheckBox;
    DBLCB_Materia: TJvDBLookupCombo;
    Label_Carrera: TLabel;
    JvSB_BuscarMateria: TJvSpeedButton;
    DS_Materias: TDataSource;
    procedure CB_FechaCreacionClick(Sender: TObject);
    procedure JvSB_FiltrarClick(Sender: TObject);
    procedure B_CerrarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure B_AgregarMateriasClick(Sender: TObject);
    procedure CB_MateriaClick(Sender: TObject);
    procedure JvSB_BuscarMateriaClick(Sender: TObject);
    procedure JvDPE_DesdeKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    procedure FiltrarLista;

    procedure LoadWindowConfiguration;
    procedure SaveWindowConfiguration;

  public
    { Public declarations }
  end;


implementation

uses DM_Persistencia, F_Funciones, R_DocumentByDate, F_BuscarMaterias;

{$R *.dfm}



procedure TF_DocumentsByDate.FiltrarLista;
var
  s:string;
begin
  JvDPE_Desde.DateFormat:= 'MM/dd/yyyy';
  JvDPE_Hasta.DateFormat:= 'MM/dd/yyyy';
  s:= 'SELECT DOCUMENTOS.ID, TITULO, CAST (CREATION_DATE AS DATE) AS CREATION_DATE, COALESCE(MATERIAS.DENOMINACION, '+QuotedStr('')+') AS DENOMINACION'+
      ' FROM DOCUMENTOS'+
      ' LEFT JOIN DOCUMENTOS_POR_MATERIA ON DOCUMENTOS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_DOCUMENTO'+
      ' LEFT JOIN MATERIAS ON MATERIAS.ID = DOCUMENTOS_POR_MATERIA.FK_ID_MATERIA'+
      ' WHERE DENOMINACION IS NOT NULL ';
  if (CB_FechaCreacion.Checked) then s:= s + ' AND CAST(CREATION_DATE AS DATE) BETWEEN ' + QuotedStr(JvDPE_Desde.Text) +' AND ' + QuotedStr(JvDPE_Hasta.Text);
  if (CB_Materia.Checked) then s:= s + ' AND MATERIAS.ID = ' + (VarToStrDef(DBLCB_Materia.KeyValue,'0'));
  s:= s + ' ORDER BY CREATION_DATE, MATERIAS.DENOMINACION, TITULO';
  with ZROQ_Documentos do
    begin
      Close;
      SQL.Clear;
      SQL.Add(s);
      Open;
      Last;
    end;
  JvDPE_Desde.DateFormat:= 'dd/MM/yyyy';
  JvDPE_Hasta.DateFormat:= 'dd/MM/yyyy';
end;

procedure TF_DocumentsByDate.CB_FechaCreacionClick(Sender: TObject);
begin
  JvDPE_Desde.Enabled:= CB_FechaCreacion.Checked;
  JvDPE_Hasta.Enabled:= CB_FechaCreacion.Checked;
end;

procedure TF_DocumentsByDate.JvSB_FiltrarClick(Sender: TObject);
begin
 FiltrarLista;
end;

procedure TF_DocumentsByDate.B_CerrarClick(Sender: TObject);
begin
  Close;
end;

procedure TF_DocumentsByDate.FormCreate(Sender: TObject);
begin
  LoadWindowConfiguration;

  ZROQ_Documentos.Open;
end;

procedure TF_DocumentsByDate.SaveWindowConfiguration;
var
  MiFichero : TIniFile;
  ruta:string;
  i:integer;
begin
  ruta:= ExtractRutaConfiguracion;
  try
    MiFichero:=TIniFile.Create(ruta);

    with JvDBUltimGrid do
      for i := 0 to (Columns.Count - 1) do
        MiFIchero.WriteInteger(Caption,Columns[i].FieldName, Columns[i].Width);

    with GB_OpcionesDeFiltrado do
      begin
        for i:=0 to ControlCount-1 do
          MiFIchero.WriteBool(Caption,TCheckBox(Controls[i]).Caption, TCheckBox(Controls[i]).Checked);
      end;

    MiFIchero.WriteInteger(Caption,'Height',Height);
    MiFIchero.WriteInteger(Caption,'Width',Width);
    MiFIchero.WriteBool(Caption, 'InitMax', WindowState = wsMaximized );

  finally
    MiFIchero.Free;
  end;
end;

procedure TF_DocumentsByDate.LoadWindowConfiguration;
var
  MiFichero : TIniFile;
  ruta:string;
  i:integer;
begin
  ruta:= ExtractRutaConfiguracion;
  try
    MiFichero:=TIniFile.Create(ruta);
    with JvDBUltimGrid do
      for i := 0 to (Columns.Count - 1) do
        Columns[i].Width:= MiFIchero.ReadInteger(Caption,Columns[i].FieldName,35);

    with GB_OpcionesDeFiltrado do
      begin
        for i:=0 to ControlCount-1 do
          TCheckBox(Controls[i]).Checked:= MiFIchero.ReadBool(Caption,TCheckBox(Controls[i]).Caption, false);
      end;

    Height:= MiFIchero.ReadInteger(Caption,'Height',0);
    Width:= MiFIchero.ReadInteger(Caption,'Width',0);

    if MiFIchero.ReadBool( Caption, 'InitMax', false ) then
      WindowState := wsMaximized
    else
      WindowState := wsNormal;
  finally
    MiFIchero.Free;
  end;
end;

procedure TF_DocumentsByDate.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveWindowConfiguration;
end;

procedure TF_DocumentsByDate.B_AgregarMateriasClick(Sender: TObject);
begin
  with TF_DocumentsByDat.Create(Self, ZROQ_Documentos) do
    try
      QR_DocumentsByDate.Preview;
    finally
      QR_DocumentsByDate.Free;
    end;
end;

procedure TF_DocumentsByDate.CB_MateriaClick(Sender: TObject);
begin
  DBLCB_Materia.Enabled:= CB_Materia.Checked;
end;

procedure TF_DocumentsByDate.JvSB_BuscarMateriaClick(Sender: TObject);
begin
  with TF_SeleccionarMateria.Create(Self) do
    begin
      if (ShowModal = mrOk) then
        DBLCB_Materia.KeyValue:= Q_Materia.FieldByName('ID').AsInteger;
      Free;
    end;
end;

procedure TF_DocumentsByDate.JvDPE_DesdeKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = #13) then
    FiltrarLista;
end;

end.
